//
//  MOAddressBookSearchViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/6/10.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOAddressBookSearchViewController.h"
#import "MOAddressBookSearchViewCell.h"
#import "MOAddressBookDetailViewController.h"

@interface MOAddressBookSearchViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

//自定义导航
@property(nonatomic,strong)UIView *navView;

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataSource;

@property(nonatomic,strong)UITextField *searchTextField;

@end

@implementation MOAddressBookSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNav];
    
    [self creatView];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

//自定义导航栏
-(void)initNav
{
    _navView = [[UIView alloc]initWithFrame:CGRectMake(0, StatusBar_HEIGHT, SCREEN_WIDTH, 16+kHConversions(32)+5)];
    _navView.backgroundColor = KWhiteColor;
    [self.view addSubview:_navView];
    
    self.searchTextField = [[UITextField alloc]initWithFrame:CGRectMake(kWConversions(13), 16, SCREEN_WIDTH - kWConversions(13)*3 - 40, kHConversions(32))];
    self.searchTextField.backgroundColor = KGrayColor_F3F3F3;
    self.searchTextField.placeholder = @"输入搜索的内容";
    self.searchTextField.delegate = self;
    self.searchTextField.font = FONT_SIZE(13);
    self.searchTextField.layer.masksToBounds = YES;
    self.searchTextField.layer.cornerRadius = 3.0;
    self.searchTextField.textColor = KBlackColor_2A2A2A;
    
    UIView *leftview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 27, kHConversions(32))];
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 23, kHConversions(32))];
    [button setImage:IMAGE(@"address_search") forState:UIControlStateNormal];
    [button setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentRight)];
    [leftview addSubview:button];
    self.searchTextField.leftView = leftview;
    self.searchTextField.leftViewMode = UITextFieldViewModeAlways;
    
    [_navView addSubview:self.searchTextField];
    
    UIButton *cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-kWConversions(13)-40, 16, 40, kHConversions(32))];
    [cancelButton setTitleColor:KBlueColor_2C94FF forState:UIControlStateNormal];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(clickCancelButton) forControlEvents:UIControlEventTouchUpInside];
    cancelButton.titleLabel.font = FONT_SIZE(15);
    [_navView addSubview:cancelButton];
    
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    [self initData];
}

#pragma mark -- 取消按钮
-(void)clickCancelButton
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -- 页面视图
-(void)creatView
{
    self.view.backgroundColor = KWhiteColor;
    
    [self.view addSubview:self.tableView];
    
    [self performSelector:@selector(openKeyboard) withObject:nil afterDelay:1];
}

-(void)openKeyboard
{
    [self.searchTextField becomeFirstResponder];
}

#pragma mark -- 数据
-(void)initData
{
    self.dataSource = [NSMutableArray array];
    
    NSDictionary *addressBook = @{
                           @"truename":self.searchTextField.text,
                           };
    
    NSString *postUrl = [NSString stringWithFormat:@"%@?authorization=%@",ADDRESSBOOK_GETABBYBYORGID_URL,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [[ADShowMessage shareShowMessage] showHUDAddedTo:self.view animated:YES];
    
    [PPNetworkHelper POST:postUrl parameters:addressBook success:^(id responseObject) {
        NSLog(@"部门人员搜索:%@",responseObject);
        
        [[ADShowMessage shareShowMessage] hideHUDForView:self.view animated:YES];
        
        NSArray *arr = responseObject;
        for (int i= 0 ; i<arr.count; i++) {
            MOAddressBookPeopleModel *model = [[MOAddressBookPeopleModel alloc]init];
            model.searchContent = self.searchTextField.text;
            model.nameStr = [NSString passNullForString:arr[i][@"truename"]];
            model.sectionStr = [NSString passNullForString:arr[i][@"depment"]];
            model.userId = [NSString passNullForString:arr[i][@"userid"]];
            model.room = [NSString passNullForString:arr[i][@"room"]];
            model.post = [NSString passNullForString:arr[i][@"post"]];
            model.tel = [NSString passNullForString:arr[i][@"tel"]];
            model.mobile = [NSString passNullForString:arr[i][@"mobile"]];
            model.ipphone = [NSString passNullForString:arr[i][@"ipphone"]];
            model.email = [NSString passNullForString:arr[i][@"email"]];
            model.ybcode = [NSString passNullForString:arr[i][@"ybcode"]];
            
            [self.dataSource addObject:model];
        }
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        [[ADShowMessage shareShowMessage] hideHUDForView:self.view animated:YES];
        
        [[ADShowMessage shareShowMessage] showTextOnly:[error localizedDescription] showInView:WINDOW];
    }];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOAddressBookSearchViewCell *cell = [MOAddressBookSearchViewCell cellWithTableView:tableView andIndexPath:indexPath];
    
    MOAddressBookPeopleModel *model = self.dataSource[indexPath.row];
    
    cell.model = model;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kHConversions(12) + kHConversions(3) + 17 + 13*2 + kHConversions(5)*2 + kHConversions(15);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.00001;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MOAddressBookDetailViewController *vc = [[MOAddressBookDetailViewController alloc]init];
    vc.addressModel = self.dataSource[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}




#pragma mark -- 懒加载
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, StatusBar_HEIGHT + _navView.height, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBar_HEIGHT - _navView.height) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = KGrayColor_F3F3F3;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}



@end
